﻿Imports System.Data.SqlClient
Public Class frmRespaldar
    Dim Bitacora As New clsActividad
    Dim Usuario As New clsUsuarioPrincipal

    Private Sub frmRespaldar_FormClosed(sender As Object, e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        Bitacora.RegistraActividad("Cerró la ventana de respaldo de base de datos")
        Usuario.Ubicacion("Ventana Principal")
    End Sub
    Private Sub frmRespaldar_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        ToolStrip1.Cursor = Cursors.Hand
        ToolStrip1.RenderMode = System.Windows.Forms.ToolStripRenderMode.System

        Bitacora.RegistraActividad("Ingresó a la ventana de respaldo de base de datos")
        Usuario.Ubicacion("Respaldo de Base de datos")

        CargaEmpresas()
        txtRespaldo.Clear()
        txtUbicacion.Clear()

    End Sub
    Sub CargaEmpresas()
        lstEmpresas.Items.Clear()

        Dim cnConn As New SqlConnection
        cnConn.ConnectionString = InitialConnection

        Dim strSql As String = "Select * from Empresas WHERE IdEmpresa>0 Order by Nombre"



        Dim cmdBuscar As New SqlCommand
        cmdBuscar.Connection = cnConn
        cmdBuscar.CommandText = strSql
        Dim rdBuscar As SqlDataReader

        cnConn.Open()
        rdBuscar = cmdBuscar.ExecuteReader

        Do While rdBuscar.Read()
            lstEmpresas.Items.Add(rdBuscar("Nombre").ToString.Trim)

        Loop
    End Sub

    Private Sub lstEmpresas_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles lstEmpresas.SelectedIndexChanged
        Dim Ubicacion As String = BuscarStrDatoInicial("Select Respaldo FROM Empresas Where Nombre='" & lstEmpresas.Text.Trim & "'")
        Dim BD As String = BuscarStrDatoInicial("SELECT BD FROM Empresas WHERE Nombre='" & lstEmpresas.Text.Trim & "'")
        txtUbicacion.Text = Ubicacion
        txtRespaldo.Text = BD & "_" & Date.Now.ToString("yyyyMMdd") & "_" & Date.Now.ToString("HHmmss") & ".bak"

    End Sub

    Private Sub ToolStripButton1_Click(sender As System.Object, e As System.EventArgs) Handles ToolStripButton1.Click
        Close()

    End Sub

    Private Sub ToolStripButton2_Click(sender As System.Object, e As System.EventArgs) Handles ToolStripButton2.Click
        Dim LargoEmpresa As Integer = Len(lstEmpresas.Text)
        If LargoEmpresa > 0 Then
            Dim BD As String = BuscarStrDatoInicial("SELECT BD FROM Empresas WHERE Nombre='" & lstEmpresas.Text.Trim & "'")
            Dim Conn As New SqlConnection("Server=" & Server.Trim & "\COMPAC;Database=" & BD & "; user id = SA; password = Compac123;")
            Conn.Open()
            Dim Command As New SqlCommand("BACKUP DATABASE [" & BD & "] TO DISK='" & txtUbicacion.Text.Trim & "\" & txtRespaldo.Text.Trim & "'", Conn)
            Try
                Command.ExecuteNonQuery()
                MsgBox("El respaldo se ha creado correctamente", MsgBoxStyle.Information, "Aviso")
                Bitacora.RegistraActividad("Respaldó la empresa " & lstEmpresas.Text.Trim)
                Close()
            Catch ex As Exception
                MsgBox(ex.Message, MsgBoxStyle.Critical)
            Finally
                Conn.Close()
            End Try
        Else
            MsgBox("Debes seleccionar una empresa", MsgBoxStyle.Exclamation, "Aviso")
        End If
    End Sub
End Class